﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Net;

namespace FinanceGoldPointDal.Utils
{
  public class QuoteUtilsYHOO : BaseStockUtil
  {

    public static readonly string QuoteUrlMainYHOO = "http://download.finance.yahoo.com/d/quotes.csv?s=";


		
    public string GetQueryXMLYHOO(string[] pSysmbols)
    {
      string query = @"select * from yahoo.finance.quotes where symbol in ({0})";
      string temp = "";
      foreach (var simb in pSysmbols)
      {
        temp += "\"" + simb + "\"";
      }

      return string.Format(query, temp);

    }

    public string GetYHOOQuerySymbol(int pLevel_0_1_2_3)
    {
      var sym = new YAHOOQuerySymbols();

      string query = "&f=";

      switch (pLevel_0_1_2_3)
      {
        case 1:
          return query + string.Join(string.Empty,
          sym.Symbol, sym.LastTradePriceOnly, sym.LastTradeTime,
          sym.Open, sym.DaysHigh, sym.DaysLow, sym.DaysRange, sym.ChangeAndPercentChange,
          sym.ChangeInPercent, sym.ChangeRealTime, sym.AverageDailyVolume);
          break;
        case 2:
          return query + string.Join(string.Empty, 
          sym.Symbol, sym.LastTradePriceOnly, sym.LastTradeTime,
          sym.Open, sym.DaysHigh, sym.DaysLow, sym.DaysRange, sym.ChangeAndPercentChange, 
          sym.ChangeInPercent, sym.ChangeRealTime,sym.AverageDailyVolume,

           sym.Volume, sym.AnnualizedGain, sym.Changefrom50DayMovingAverage,
          sym.Changefrom200DayMovingAverage, sym.Changefrom50DayMovingAverage, sym.AfterHoursChangeRealTime,          
          sym.DividendShare, sym.FloatShares, sym.HoldingsGainPercentRealtime, sym.OrderBookRealtime,
          sym.MarketCapRealtime, sym.In52WeekHigh, sym.In52WeekLow,
          sym.EBITDA, sym.PreviousClose, sym.PEGRatio          
          );
          break;
        default:
          return string.Empty;
          return query + string.Join(string.Empty, "s", "l1", "t1", "o", "h", "g", "c", "c6", "a2", "m7");
          break;
          break;
      }

    }


   



    public string getYHQueryStatBase(string pCode)
    {
      return string.Format("http://it.finance.yahoo.com/q/ks?s={0}", pCode);
    }

    public string getYHQueryStoricoTitolo(string pCodice, DateTime pdal, DateTime pAl)
    {
      return string.Format("http://it.finance.yahoo.com/q/hp?s={0}&b={1}&a={2}&c={3}&e={4}&d={5}&f={6}&g=d",
        pCodice, pdal.Day, pdal.Month, pdal.Year, pAl.Day, pAl.Month, pAl.Year);

    }



  

    public FieldInfo[] GetClassFields<T>()
    {
      var tipo = typeof(T);
      var props = tipo.GetFields();
      return props;
    }//fM

    public PropertyInfo[] GetClassProps<T>()
    {
      var tipo = typeof(T);
      var props = tipo.GetProperties();
      return props;
    }//fM

    public string GetStringFullYHOOQuery() 
    {
      var fields = GetClassFields<YAHOOQuerySymbols>();
      var item = new YAHOOQuerySymbols();

      string query = "";
      foreach (var field in fields)
      {
        query+= field.GetValue(item); 
      }

      return "&f=" + query;
    }//fM


  

    public T GetFullYHOOQuoteClass<T>(string[] pValues) where T : class, new()
    {
      var fields = GetClassProps<T>();
      T item = new T();

      int conta =0;
      string query = "";
      foreach (var field in fields)
      {
        field.SetValue(item, pValues[conta],null);
        ++conta;
      }

      return item;
    }//fM


 
  

  }
}
